package edu.vital.energy.longevity.ch09.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Classname LoginController
 * @Description 登录控制器
 * @Date 2019-4-10 11:29
 * @Created by CrazyStone
 */
@Controller
public class LoginController {
    /**
     * 获取并封装当前年份跳转到登录页login.html
     * @param model
     * @return
     */
    @GetMapping("/toLoginPage")
    public String toLoginPage(Model model){
        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "login";
    }
    
    /**
     * 处理登录请求
     * @param username 用户名
     * @param password 密码
     * @param session HttpSession对象
     * @param model Model对象
     * @return 登录成功跳转到index页面，失败返回login页面
     */
    @PostMapping("/login")
    public String login(@RequestParam("username") String username, 
                       @RequestParam("password") String password,
                       HttpSession session,
                       Model model) {
        System.out.println(getUser().get(username));
        Map<String, String> users = getActiveUsers(session);
        // 简单的验证逻辑，实际应用中应使用数据库验证
//        if ("admin".equals(username) && "123456".equals(password)) {
        if (getUser().get(username).equals(password)) {
            // 登录成功，将用户信息存入session
            users.put(username,username);
            session.setAttribute("activeUsers", users);
            model.addAttribute("loginUser", username);
            // 跳转到首页
            return "index";
        } else {
            System.out.println(2);
            // 登录失败，返回登录页面并显示错误信息
            model.addAttribute("msg", "用户名或密码错误");
            model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
            return "login";
        }
    }
    private Map<String, String> getActiveUsers(HttpSession session) {
        Map<String, String> users = (Map<String, String>) session.getAttribute("activeUsers");
        return users != null ? users : new ConcurrentHashMap<>();
    }    /**
     * 处理注销请求
     * @param session HttpSession对象
     * @return 重定向到登录页
     */
    @GetMapping("/logout")
    public String logout(@RequestParam("loginUser") String username,HttpSession session) {
        // 清除session中的用户信息
        Map<String, String> users = getActiveUsers(session);
        System.out.println(username);
        users.remove(username);
//        session.removeAttribute("activeUsers");
        session.setAttribute("activeUsers",users);
        // 重定向到登录页
        return "redirect:toLoginPage";
    }

    public Map<String,String> getUser(){
        Map<String,String> map=new HashMap<String,String>();
        map.put("admin","123456");
        map.put("root","123456");
        map.put("master","123456");
        return  map;
    }
}
